package com.qfdu.repo;

import com.qfdu.repo.bean.Manager;
import com.qfdu.repo.db.DBUtils;
import com.qfdu.repo.db.PstCallback;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 沐辰
 * @version v1.0
 * @description:
 * @project Qfed2401
 * @Date 2024/5/8 10:14
 * @company qf
 */
public class Main {
    public static void main(String[] args) {
//        update();
        query();
    }
    public static void update(){
        String sql = "insert into manager (account,password,phone,level,remark) values (?,?,?,?,?)";
        final Manager manager = new Manager();
        manager.setAccount("admin");
        manager.setPassword("123456");
        manager.setPhone("13935285842");
        manager.setLevel("level2");
        manager.setRemark("二级管理员");
        int result = DBUtils.executeUpdate(sql, new PstCallback() {
            public void pstSetValue(PreparedStatement pst) {
                try {
                    pst.setString(1,manager.getAccount());
                    pst.setString(2,manager.getPassword());
                    pst.setString(3,manager.getPhone());
                    pst.setString(4,manager.getLevel());
                    pst.setString(5,manager.getRemark());
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
        });
        System.out.println(result);
        DBUtils.getClose();
    }
    public static void query(){
        String sql = "select * from manager";
        ResultSet resultSet = DBUtils.query(sql, new PstCallback() {
            public void pstSetValue(PreparedStatement pst) {

            }
        });
        List<Manager> list = new ArrayList<Manager>();
        try {
            while (resultSet.next()) {
                int id = resultSet.getInt("id");
                String account = resultSet.getString("account");
                String password = resultSet.getString("password");
                String phone = resultSet.getString("phone");
                String level = resultSet.getString("level");
                String remark = resultSet.getString("remark");
                Manager manager = new Manager();
                manager.setId(id);
                manager.setAccount(account);
                manager.setPassword(password);
                manager.setPhone(phone);
                manager.setLevel(level);
                manager.setRemark(remark);
                list.add(manager);
            }
            System.out.println(list.size());
        }catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
